package com.bajiao.admin.controller.account;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.bajiao.vo.account.AccountAdmin;
import com.bajiao.vo.account.AccountBillAdmin;
import com.github.pagehelper.PageHelper;
import com.bajiao.common.api.vo.Result;
import com.bajiao.common.system.vo.LoginUser;
import com.bajiao.common.web.BaseController;
import com.bajiao.entity.account.AccountBill;
import com.bajiao.service.account.AccountBillService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: 账单
 * @Author: FanJq
 * @Date: 2024-10-11
 * @Version: V1.0
 */
@Slf4j
@Api(tags = "账单")
@RestController
@RequestMapping("/account/bill")
public class AccountBillController extends BaseController {

    @Resource
    private AccountBillService accountBillService;

    @ApiOperation(value = "账单-分页列表查询", notes = "账单-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(String userId, String nickname, Integer billType, Integer type, String createTimeBegin, String createTimeEnd) {
        Map<String, Object> params = new HashMap<>();
        if (StrUtil.isNotBlank(userId))
            params.put("userId", userId);

        if (StrUtil.isNotBlank(nickname))
            params.put("nickname", nickname);

        if (ObjectUtil.isNotNull(billType))
            params.put("billType", billType);

        if (ObjectUtil.isNotNull(type))
            params.put("type", type);

        if (StrUtil.isAllNotBlank(createTimeBegin, createTimeEnd)) {
            params.put("createTimeBegin", createTimeBegin);
            params.put("createTimeEnd", createTimeEnd + " 23:59:59");
        }
        startPage();
        return Result.ok(getDataTable(accountBillService.getAdminList(params)));
    }

    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(String userId, String nickname, Integer billType, Integer type, String createTimeBegin, String createTimeEnd, String fileName) {
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        Map<String, Object> params = new HashMap<>();
        if (StrUtil.isNotBlank(userId))
            params.put("userId", userId);

        if (StrUtil.isNotBlank(nickname))
            params.put("nickname", nickname);

        if (ObjectUtil.isNotNull(billType))
            params.put("billType", billType);

        if (ObjectUtil.isNotNull(type))
            params.put("type", type);

        if (StrUtil.isAllNotBlank(createTimeBegin, createTimeEnd)) {
            params.put("createTimeBegin", createTimeBegin);
            params.put("createTimeEnd", createTimeEnd + " 23:59:59");
        }
        // Step.2 获取导出数据
        List<AccountBillAdmin> exportList = accountBillService.getAdminList(params);
        // Step.3 AutoPoi 导出Excel
        ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
        mv.addObject(NormalExcelConstants.FILE_NAME, fileName); //此处设置的filename无效 ,前端会重更新设置一下
        mv.addObject(NormalExcelConstants.CLASS, AccountBillAdmin.class);
        mv.addObject(NormalExcelConstants.PARAMS, new ExportParams(fileName, "导出人:" + sysUser.getRealname(), fileName));
        mv.addObject(NormalExcelConstants.DATA_LIST, exportList);
        return mv;
    }
}
